import { defHttp } from '@/utils/http/axios';
import {
  DictTreeItemModel,
  FieldManageItemList,
  FieldManageItemParams,
  FieldRelationItemList,
  FieldRelationItemParams,
  MessageSourceItemList,
  MessageSourcePageParams,
  MessageTargetItemList,
  MessageTargetItemParams,
  SelectGroupItem,
} from '@/api/resource/model/resourceAllModel';

const basePath = '/resource';
const buildUrl = (url: string) => basePath + url;
export function MessageSource() {
  enum Api {
    PageList = '/resMessageSource/pageQuery',
    QueryAllByCloud = '/resMessageSource/queryAllByCloud',
    QueryAllByTarget = '/resMessageTarget/queryAll',
    AddOrUpdate = '/resMessageSource/addOrUpdate',
    LeftTree = '/resScriptManage/cloudTypeCascade',
    ScriptAllByCloud = '/resScriptManage/queryAllByCloud',
    Del = '/resMessageSource/deleteById',
  }

  const leftTree = () => defHttp.get<DictTreeItemModel>({ url: buildUrl(Api.LeftTree) });

  const pageList = (params: MessageSourcePageParams) =>
    defHttp.post<MessageSourceItemList>({
      url: buildUrl(Api.PageList),
      params: { page: params.page, pageSize: params.pageSize, t: params },
    });

  const addOrUpdate = (params: any) => defHttp.post({ url: buildUrl(Api.AddOrUpdate), params });

  const queryAllByCloud = (params: any) =>
    defHttp.post({ url: buildUrl(Api.QueryAllByCloud), params });

  const queryAllByTarget = () => defHttp.get({ url: buildUrl(Api.QueryAllByTarget) });

  const del = (id: string) => defHttp.get({ url: buildUrl(Api.Del) + '/' + id });
  const scriptAllByCloud = (param: string) =>
    defHttp.get<Map<string, SelectGroupItem[]>>({
      url: buildUrl(Api.ScriptAllByCloud) + '/' + param,
    });
  return {
    leftTree,
    pageList,
    addOrUpdate,
    del,
    queryAllByCloud,
    scriptAllByCloud,
    queryAllByTarget,
  };
}

export function MessageTarget() {
  enum Api {
    PageList = '/resMessageTarget/pageQuery',
    AddOrUpdate = '/resMessageTarget/addOrUpdate',
    Del = '/resMessageTarget/deleteById',
  }
  const pageList = (params: MessageTargetItemParams) =>
    defHttp.post<MessageTargetItemList>({
      url: buildUrl(Api.PageList),
      params: { page: params.page, pageSize: params.pageSize, t: params },
    });

  const addOrUpdate = (params: any) => defHttp.post({ url: buildUrl(Api.AddOrUpdate), params });

  const del = (id: string) => defHttp.get({ url: buildUrl(Api.Del) + '/' + id });

  return {
    pageList,
    addOrUpdate,
    del,
  };
}

export function FieldManage() {
  enum Api {
    PageList = '/resFieldsManage/pageQuery',
    AddOrUpdate = '/resFieldsManage/addOrUpdate',
    Del = '/resFieldsManage/deleteById',
  }
  const pageList = (params: FieldManageItemParams) =>
    defHttp.post<FieldManageItemList>({
      url: buildUrl(Api.PageList),
      params: { page: params.page, pageSize: params.pageSize, t: params },
    });

  const addOrUpdate = (params: any) => defHttp.post({ url: buildUrl(Api.AddOrUpdate), params });

  const del = (id: string) => defHttp.get({ url: buildUrl(Api.Del) + '/' + id });

  return {
    pageList,
    addOrUpdate,
    del,
  };
}
export function FieldRelation() {
  enum Api {
    PageList = '/resFieldsReflection/pageQuery',
    AddOrUpdate = '/resFieldsReflection/addOrUpdate',
    QueryScriptBySourceId = '/resFieldsReflection/queryScriptBySourceId',
    Del = '/resFieldsReflection/deleteById',
    QueryFieldList = '/resFieldsManage/queryByResId',
  }
  const pageList = (params: FieldRelationItemParams) =>
    defHttp.post<FieldRelationItemList>({
      url: buildUrl(Api.PageList),
      params: { page: params.page, pageSize: params.pageSize, t: params },
    });

  const addOrUpdate = (params: any) => defHttp.post({ url: buildUrl(Api.AddOrUpdate), params });

  const del = (id: string) => defHttp.get({ url: buildUrl(Api.Del) + '/' + id });

  const queryFieldList = (sourceId: string) =>
    defHttp.get({ url: buildUrl(Api.QueryFieldList) + '/' + sourceId });
  const queryScriptBySourceId = (sourceId: string) =>
    defHttp.get({ url: buildUrl(Api.QueryScriptBySourceId) + '/' + sourceId });
  return {
    pageList,
    addOrUpdate,
    del,
    queryFieldList,
    queryScriptBySourceId,
  };
}
